#!/bin/bash

nginx_home=/opt/module/nginx
log_home=/opt/gmall
log_jar=gamll_logger-0.0.1-SNAPSHOT.jar

case $1 in
"start")
if [[ -z "`ps -ef | awk '/nginx/ && !/awk/ {print $n}'`" ]]; then
    echo "正在启动nginx -> hadoop162"
    $nginx_home/sbin/nginx
else
    echo "nginx已经启动了~"
fi

for host in hadoop162 hadoop163 hadoop164
do
    echo "正在启动日志服务器 -> $host"
    ssh $host "nohup java -jar $log_home/$log_jar 1>/dev/null 2>$log_home/run.log &"
done
;;

"stop")
if [[ -z "`ps -ef | awk '/nginx/ && !/awk/ {print $n}'`" ]]; then
    echo "nginx已经停了~"
else
    echo "正在停止nginx -> hadoop162"
    $nginx_home/sbin/nginx -s stop
fi

if [[ -z "`jps | grep $log_jar | grep -v grep`" ]];then
  echo "日志服务器已经停了~"
  else
for host in hadoop162 hadoop163 hadoop164
do
  echo "正在停止日志服务器 —> $host"
  ssh $host "jps | awk '/$log_jar/ {print \$1}' | xargs kill -9"
done
fi
;;

*)
echo "你启动的姿势不对, 换个姿势再来"
echo " log.sh start 启动日志采集"
echo " log.sh stop  停止日志采集"
;;
esac
